import { get } from '@common/ajax';
import logger from '@common/logger';
import { ClearTip, TipLoading, Tip } from '@common/tip';
import { SHOP_URL, MARKET_URL } from '@common/url';
import { MARKET_ISSUER, MARKET_DISTRIBUTE_SCENE } from '@constants/market';
import { dealStoreConfig } from '@biztools/special';
import { dealDiscountInfo } from '@biztools/detail';

import {
    SET_STORE_ID,
    SET_STORE_INFO,
    SET_STORE_CONFIG,
    GET_STORE_CATEGORY,
    SET_STORE_SHELF_PARAM,
    GET_STORE_SHELF_LIST,
    GET_STORE_DISCOUNT_INFO,
} from '@store/mutationType';

const special = {
    state: {
        storeId: 0,
        storeInfo: null, // 店铺信息
        contenList: [],
        activityList: [],
        couponList: [],
        categoryList: [], // 分类
        shelfList: [], // 货架数据
        // 分页参数
        categoryId: 0,
        searchValue: '',
        pageNumber: 1,
        pageSize: 10,
    },
    mutations: {
        SET_STORE_ID: (state, storeId) => {
            Object.assign(state, { storeId });
        },
        SET_STORE_INFO: (state, storeInfo) => {
            Object.assign(state, { storeInfo });
        },
        SET_STORE_CONFIG: (state, contenList) => {
            Object.assign(state, { contenList });
        },
        GET_STORE_CATEGORY: (state, categoryList) => {
            Object.assign(state, { categoryList });
        },
        SET_STORE_SHELF_PARAM: (state, { categoryId, pageNumber }) => {
            Object.assign(state, { categoryId, pageNumber });
        },
        GET_STORE_SHELF_LIST: (state, shelfList) => {
            Object.assign(state, { shelfList });
        },
        GET_STORE_DISCOUNT_INFO: (state, { activityList, couponList }) => {
            Object.assign(state, { activityList, couponList });
        },
    },
    actions: {
        // 设置门店信息
        setStoreInfo: ({ commit, dispatch }, storeId) => {
            if (storeId) {
                dispatch('getStoreConfig', storeId);
            } else {
                commit(SET_STORE_INFO, null);
            }
        },
        // 获取门店装修信息
        getStoreConfig: async ({ commit }, storeId) => {
            commit(SET_STORE_ID, storeId);
            logger.info('Special => 获取门店装修信息', storeId);
            const res = await get(SHOP_URL.config, {
                id: storeId,
            });
            logger.success('Special => 门店装修信息', res);
            const { storeInfo, contenList } = dealStoreConfig(res, storeId);
            logger.success('Special => 处理后信息', storeInfo, contenList);
            commit(SET_STORE_INFO, storeInfo);
            commit(SET_STORE_CONFIG, contenList);
        },
        // 获取门店可用的优惠信息-促销 & 优惠券
        getGoodsDiscountInfo: async ({ commit, state }) => {
            const { storeId } = state;
            const res = await get(MARKET_URL.specialDiscount, {
                owner_id: storeId,
                issuer: MARKET_ISSUER.emall,
            });
            const { activityList, couponList } = dealDiscountInfo(res.data.offers);
            logger.debugger('Detail => 获取的活动和优惠券为：', activityList, couponList);
            commit(GET_STORE_DISCOUNT_INFO, {
                activityList,
                couponList,
            });
        },
        // 获取分类数据
        getStoreCategory: async ({ commit, dispatch }, storeId) => {
            commit(SET_STORE_ID, storeId);
            const res = await get(SHOP_URL.group, { id: storeId });
            commit(GET_STORE_CATEGORY, [{
                id: 0,
                name: '全部',
            }, ...res.groups]);
            dispatch('getStoreShelf', 0);
        },
        // 获取货架数据
        getStoreShelf: async ({ commit, state }, categoryId) => {
            const { pageSize, storeId, searchValue } = state;
            const param = {
                id: storeId,
                group_id: categoryId,
                query: searchValue,
                page_no: 1,
                page_size: pageSize,
            };
            commit(SET_STORE_SHELF_PARAM, { categoryId, pageNumber: 1 });
            const res = await get(SHOP_URL.shelf, param);
            commit(GET_STORE_SHELF_LIST, res.prods);
        },
        // 获取货架数据-下一页
        getStoreShelfNext: async ({ commit, state }) => {
            const { categoryId, pageNumber, pageSize, storeId, shelfList, searchValue } = state;
            const pageNo = pageNumber + 1;
            const param = {
                id: storeId,
                group_id: categoryId,
                query: searchValue,
                page_no: pageNo,
                page_size: pageSize,
            };
            const res = await get(SHOP_URL.shelf, param);
            const result = [...shelfList, ...res.prods];
            commit(SET_STORE_SHELF_PARAM, { pageNumber: pageNo, categoryId });
            commit(GET_STORE_SHELF_LIST, result);
            res.prods.length === 0 && Tip('已加载全部数据');
        },
    },
};

export default special;
